<template>
  <div class="mood-edit">
    <nav-back />
    <div class="moods">
      <span
        v-for="(v,i) in moods"
        :key="i"
        class="mood"
        @click="updateMoodCode(v.code)"
      >
        <i class="icon" v-show="v.code!==mood" :style="{backgroundImage:`url('${v.icon}')`}"></i>
        <i class="icon" v-show="v.code===mood" :style="{backgroundImage:`url('${v.iconActive}')`}"></i>
      </span>
    </div>
    <div class="article">
      <textarea
        id="text"
        rows="10"
        :maxlength="max"
        v-model="text"
      ></textarea>
      <span class="len">{{len}}</span>
    </div>
    <div class="submit" @click="submit">发布心情</div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from 'vuex'
import { NavBack } from '@/components'
import { getMoodIcon } from '@/tools'
const { mapMutations } = createNamespacedHelpers('Mood')
export default {
  name: 'mood-edit',
  components: { NavBack },
  data: () => ({
    moods: [
      { code: 1, icon: getMoodIcon(1).icon, iconActive: getMoodIcon(1).iconActive },
      { code: 2, icon: getMoodIcon(2).icon, iconActive: getMoodIcon(2).iconActive },
      { code: 3, icon: getMoodIcon(3).icon, iconActive: getMoodIcon(3).iconActive },
      { code: 4, icon: getMoodIcon(4).icon, iconActive: getMoodIcon(4).iconActive },
      { code: 5, icon: getMoodIcon(5).icon, iconActive: getMoodIcon(5).iconActive }
    ],
    mood: 0,
    text: '',
    max: 100
  }),
  computed: {
    len() {
      return `${this.text.length}/${this.max}`
    }
  },
  methods: {
    updateMoodCode(code) {
      this.mood = code
    },
    ...mapMutations(['updateMood']),
    submit() {
      if (!!this.text.trim() && this.mood) {
        this.updateMood({ code: this.mood, article: this.text })
        this.$router.back()
      }
    }
  },
  beforeRouteEnter(from, to, next) {
    next(vm => {
      vm.mood = 0
      vm.text = ''
    })
  }
}
</script>

<style lang="stylus">
$icon-w = 88px;
$icon-h = 94px;
$scale = 0.5;

.mood-edit {
  height: 100vh;
  background-color: #fff;

  .moods {
    display: flex;
    margin: 16px;
    border-bottom: 1px solid $border-color;
  }

  .mood {
    margin-right: 10px;
  }

  .icon {
    display: block;
    width: $scale * $icon-w;
    height: $scale * $icon-h;
    background: center / cover no-repeat;
  }

  .article {
    position: relative;
    margin: 16px;
  }

  #text {
    width: 100%;
    padding: 10px;
    resize: none;
    box-sizing: border-box;
    font-size: 16px;
  }

  .len {
    position: absolute;
    bottom: 10px;
    right: 10px;
    display: block;
  }

  .submit {
    width: 80%;
    margin: 10px auto;
    border-radius: 5px;
    padding: 10px 0;
    text-align: center;
    background-color: $main-color;
    color: #ffffff;
  }
}
</style>
